package com.example.cia;

import java.io.IOException;
import java.sql.SQLException;

import com.example.cia.pojo.user;
import com.example.cia.support.IonClick;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class aboutApp extends Activity implements IonClick{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about);
		
		Button userInterface = (Button) findViewById(R.id.loginOrRegist);
		setUserInterface(userInterface);
		
		userInterface.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				showMainDialog();
			}
			
		});
	}

	public void setUserInterface(Button b){
		appApplication mApplication = (appApplication) getApplication();
		if(ifLogined()){
			b.setText(mApplication.getLoginUser().getUser_id()+"，欢迎您！");
		}else{
			b.setText("请登录！");
		}
	}
	
	public boolean ifLogined(){
		appApplication mApplication = (appApplication) getApplication();
		if(mApplication.getLoginUser().getUser_id()==0){
			return false;
		}
		return true;
	}
	
	public void showMainDialog(){
		AlertDialog.Builder mainDialog = new AlertDialog.Builder(aboutApp.this);
		mainDialog.setTitle("选择登录或注册");
		mainDialog.setPositiveButton("登录", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				showLoginDialog();				
			}
			
		});
		mainDialog.setNeutralButton("注册", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				showRegistDialog();
			}
		});
		mainDialog.setNegativeButton("关闭", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
		mainDialog.show();
	}
	
	@SuppressLint("InflateParams")
	public void showLoginDialog(){
		/*AlertDialog.Builder loginDialog = new AlertDialog.Builder(aboutApp.this);
		loginDialog.setTitle("登录");
		
		//使用LayoutInflater获取login_dialog.xml
        LayoutInflater factory = LayoutInflater.from(aboutApp.this);  
        //通过反射获取View
        final View loginView = factory.inflate(R.layout.login_dialog, null);
        
        loginDialog.setView(loginView);
        loginDialog.setPositiveButton("登录", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				EditText id = (EditText) findViewById(R.id.loginUserId);
				EditText password = (EditText) findViewById(R.id.loginUserPassword);
				try {
					boolean b = new userImpl().login(Integer.parseInt(id.getText()
							.toString()),password.getText().toString());
					if(b){
						user User = new user();
						User.setUser_id(Integer.parseInt(id.getText().toString()));
						User.setUser_password(password.getText().toString());
						User.setUser_address(new userImpl().getAddress(User.getUser_id()));
						appApplication mApplication = (appApplication) getApplication();
						mApplication.userLogin(User);
					}
					else{
						Toast toast = Toast.makeText(getApplicationContext(),
							     "账户名或密码错误！", Toast.LENGTH_LONG);
							   toast.setGravity(Gravity.CENTER, 0, 0);
							   toast.show();
					}
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (SQLException e) {	
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
        loginDialog.setNegativeButton("关闭", new DialogInterface.OnClickListener(){
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
        loginDialog.show();
        */
	}
	
	/*
	 * 注册暂未开放
	 */
	public void showRegistDialog(){
		AlertDialog.Builder registDialog = new AlertDialog.Builder(aboutApp.this); 
		registDialog.setTitle("注册");
		registDialog.setMessage("注册功能暂不开放");
		
		registDialog.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
		registDialog.show();
	}
	
	public void checkDeviceOnClick(View v){
		Intent intent = new Intent(this,checkDevice.class);
		startActivity(intent);
	}
	
	public void addDeviceOnClick(View v){
		Intent intent = new Intent(this,addDevice.class);
		startActivity(intent);
	}
	
	public void aboutAppOnClick(View v){

	}

	public void mainPageOnClick(View v) {
		Intent intent = new Intent(this,MainActivity.class);
		startActivity(intent);
	}  
	

}
